package com.njy.aop.test;

import java.util.concurrent.*;

public class FutureTask2 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        FutureTask<String> f2 = new FutureTask<String>(new task2());
        FutureTask<String> f1 = new FutureTask<String>(new task1(f2));

        executorService.submit(f1);
        executorService.submit(f2);

        executorService.shutdown();
    }

    private static class task1 implements Callable<String> {
        private FutureTask<String> t2 ;
        public task1(FutureTask<String> p0) {
            this.t2 = p0;
        }

        @Override
        public String call() throws Exception {
            System.out.println("t1洗水壶");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("t1烧开水");
            TimeUnit.SECONDS.sleep(15);
            String re  = t2.get();
            System.out.println("拿到"+re+"开始泡茶");
            return "上茶";
        }
    }

    private static class task2 implements java.util.concurrent.Callable<String> {

        @Override
        public String call() throws Exception {
            System.out.println("t2洗茶壶");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("t2洗茶杯");
            TimeUnit.SECONDS.sleep(2);
            System.out.println("t2拿茶叶");
            TimeUnit.SECONDS.sleep(1);
            return "绿茶";
        }
    }


}
